package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.EmailRecordDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.EmailRecord;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmailRecordServiceImpl implements EmailRecordService {

    @Autowired
    private EmailRecordDao emailRecordDao;


    @Override
    public Result getAllEmailRecord(Integer emailResult, Integer page, Integer pageSize) {
        try {
            if (page == null || !CommonUtils.isNumeric(page.toString())) {
                return ResultUtil.error(500, "错误参数page", "Error Param page", null);
            }
            if (pageSize == null || !CommonUtils.isNumeric(pageSize.toString())) {
                return ResultUtil.error(500, "错误参数pageSize", "Error Param pageSize", null);
            }
            int begin = (page - 1) * pageSize;
            List<EmailRecord> list1 = emailRecordDao.getAllEmailRecord(emailResult, begin, pageSize);
            List<EmailRecord> list2 = emailRecordDao.getAllEmailRecord(emailResult, null, null);
            return ResultUtil.tableSuccess(page,pageSize,list2.size(),list1);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public void addEmailRecord(EmailRecord emailRecord) {
        try{
            emailRecord.setCreateTime(new Date());
            emailRecordDao.addEmailRecord(emailRecord);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
